<template>
  <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
  >
    <el-form-item label="用户名称" prop="userName">
      <el-input v-model="ruleForm.userName" />
    </el-form-item>


    <el-form-item label="密码" prop="password">
      <el-input v-model="ruleForm.password" type="password" show-password />
    </el-form-item>


    <el-form-item>
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        登录
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import{read} from '../Until/axiosHelp'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { useCounterStore } from '@/stores/counter'
import router from '@/router';
const store=useCounterStore();
interface RuleForm {
  "userName": "",
  "password": ""
}

const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  "userName": "",
  "password": ""
})

const locationOptions = ['Home', 'Company', 'School']

const rules = reactive<FormRules<RuleForm>>({
  userName: [
    { required: true, message: '用户名称不能为空', trigger: 'blur' },
   
  ],
  password: [
    {
      required: true,
      message: '密码不能为空',
      trigger: 'change',
    },
  ],
  
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!')
      read.post("api/Query/UserLogin",ruleForm).then(res=>{
        if(res.data.code==100)
      {
        store.staves.roleId=res.data.data.roleId
        store.staves.roleName=res.data.data.roleName
        store.staves.token=res.data.data.token
        store.staves.userId=res.data.data.userId
        store.staves.userName=res.data.data.userName
        store.staves.userNikename=res.data.data.userNikename
        ElMessage.success(res.data.msg)
        router.push("/main")
      }
      else{
        ElMessage.error(res.data.msg)
      }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

const options = Array.from({ length: 10000 }).map((_, idx) => ({
  value: `${idx + 1}`,
  label: `${idx + 1}`,
}))
</script>
<style>
@media (min-width: 1024px) {
  body {
    display: flex;
    place-items: center;
  }
}
</style>
